package com.frikinet.lifeguard;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.frikinet.lifeguard.gui.BreadCrumbs;
import com.frikinet.lifeguard.gui.CalendarTreeExplorer;
import com.frikinet.lifeguard.gui.ViewersPanel;
import com.frikinet.lifeguard.gui.dialogs.UserAdminDialog;
import com.frikinet.lifeguard.model.User;
import com.frikinet.lifeguard.services.LocalHttpServer;
import com.frikinet.lifeguard.util.HibernateUtil;
import com.frikinet.tools.gui.ActionHandler;
import com.frikinet.tools.gui.FMenuBar;
import com.frikinet.tools.gui.FMenuBuilder;
import com.frikinet.tools.gui.FOptionPane;

public class Lifeguard extends JFrame
{
	private static final long serialVersionUID = 1L;
	private static Logger log = LoggerFactory.getLogger(Lifeguard.class);
	
	private final int WIDTH = 1024;
	private final int HEIGHT = 768;
	
	private ActionHandler actionHandler;
	
	JTabbedPane tabs;
	
	JButton historyBack, historyForward;
	
	public Lifeguard()
	{
		super("Lifeguard");
		
		actionHandler = new ActionHandler(this);

		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch (Exception e)
		{
			System.err.println("Error al cargar UI: " + e.getMessage());
		}
		
		//setup Frame
		buildGUI();
		buildMenu();
		
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				action_system_exit();
			}
		});
		
		// set position center on screen
		pack();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((d.width - WIDTH) / 2, (d.height - HEIGHT) / 2);
		setSize(WIDTH, HEIGHT);

		setVisible(true);
	}
	
	private void buildGUI()
	{
		JPanel contentPane = new JPanel(new BorderLayout());
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setDividerLocation(300);
		splitPane.setOneTouchExpandable(false);
		
		tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
		tabs.addTab("Calendar", new CalendarTreeExplorer());
		splitPane.setLeftComponent(tabs);
		
		JPanel right = new JPanel(new BorderLayout());
		right.add(new BreadCrumbs(), BorderLayout.NORTH);
		right.add(new ViewersPanel(), BorderLayout.CENTER);
		splitPane.setRightComponent(right);
		
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		setContentPane(contentPane);
	}
	
	private void buildMenu()
	{
		FMenuBuilder menuBuilder = new FMenuBuilder(actionHandler, App.i18n);
		FMenuBar menuBar = menuBuilder.build(Lifeguard.class.getResourceAsStream("gui/menu.xml"), FMenuBar.class);
		menuBar.translate();
		setJMenuBar(menuBar);
	}
	
	public void action_system_exit()
	{
		if(FOptionPane.showConfirmDialog(this, App.i18n, "msg.exit"))
		{
			if(App.events.isModelChanged())
			{
				log.trace("Shutdown and compact HSQL...");
				HibernateUtil.getSession().createSQLQuery("SHUTDOWN COMPACT").executeUpdate();
			}
			System.exit(0);
		}
	}
	
	public void action_admin_users()
	{
		new UserAdminDialog(this);
	}
	
	public static void main(String args[]) throws Exception
	{
		PropertyConfigurator.configure(Lifeguard.class.getClassLoader().getResource("log4.properties"));
		App.users = new TreeSet<User>(HibernateUtil.getElements(User.class));
		App.server = new LocalHttpServer();
		App.app = new Lifeguard();
	}
}
